/**
 * Javascript file for sources needed at status.php
 */

dojo.require("dijit.layout.TabContainer");
dojo.require("dojox.layout.ContentPane");
dojo.require("dijit.form.Select");
dojo.require("dijit.Tooltip");
dojo.require("dijit.form.NumberTextBox");
dojo.require("dojo.parser"); 

var last_planets_load = 0;

function galaxytool_select_galaxy(galaxy) {
	dojo.byId("galaxy_status_content").innerHTML = '<img src="../images/loading_small.gif">';
	var source_url = "ajax/ajax_status_galaxy.php?type=get_galaxy&galaxy="+galaxy;
	var xhrArgs = {
	        url: source_url,
	        handleAs: "text",
	        load: function(textData) {
        		dojo.byId("galaxy_status_content").innerHTML = textData;
    			
    			dojo.parser.parse(dojo.byId("galaxy_status_content"));
    			
    			dojo.query(".startsHidden")
    			.style("opacity", 100)
    			.removeClass("startsHidden")
    			;

	        },
	        error: function(error) {
				console.log("Error:");
				console.log(error);
	        }
	}	
	dojo.xhrPost(xhrArgs);
	
}

function galaxytool_planet_changed() {
	if (dijit.byId("planets").get("state") == "Error" || dijit.byId("planets").get("state") == "Incomplete") {
		return;
	}
	
	var planets = dijit.byId("planets").get("value");
	if (planets == last_planets_load) return;
	
	dojo.byId("planets_info").innerHTML = '<img src="../images/loading_small.gif">';
	var source_url = "ajax/ajax_status_planets.php?planets="+planets;
	var xhrArgs = {
	        url: source_url,
	        handleAs: "text",
	        load: function(textData) {
	        	dojo.byId("planets_info").innerHTML = textData;
	    		last_planets_load = planets;
	        },
	        error: function(error) {
				console.log("Error:");
				console.log(error);
	        }
	}	
	dojo.xhrPost(xhrArgs);
	
}

function galaxytool_handle_enter(e) {
	if (e == null || e.keyCode != dojo.keys.ENTER) {
		return; // ignore onKeyPress event if other keys than ENTER are hit
	}
	galaxytool_planet_changed();
}

function galaxytool_status_scroll_galaxy(up) {
	var galaxy = dijit.byId('reload_status_tab2').get('value');
	if (up == true) {
		galaxy++;
	} else {
		galaxy--;
	}
	if (galaxy > 0) {
		dijit.byId('reload_status_tab2').set('value',galaxy); // triggers reload of galaxy
	}
}

function cursorevent(event) {
	var choosen_tab = window.location.hash;
	if (choosen_tab != "#select_tab2") return;
	if(event.keyCode == dojo.keys.LEFT_ARROW) {
		var galaxy = dijit.byId('reload_status_tab2').get('value');
		galaxy--;
		if (galaxy > 0) {
			dijit.byId('reload_status_tab2').set('value',galaxy); // triggers reload of galaxy
		}
	}
	if(event.keyCode == dojo.keys.RIGHT_ARROW) {
		var galaxy = dijit.byId('reload_status_tab2').get('value');
		galaxy++;
		dijit.byId('reload_status_tab2').set('value',galaxy); // triggers reload of galaxy
	}
}

function galaxytool_tab2_loaded() {
	galaxytool_select_galaxy(dijit.byId('reload_status_tab2').get('value'));
}

function galaxytool_status_init() {
	// select user defined tab
	var choosen_tab = window.location.hash;
	if (choosen_tab != "") {
		try {
			choosen_tab = choosen_tab.replace(/#select_/, "");
			dijit.byId("tabcontainer").selectChild(dijit.byId(choosen_tab));
		} catch (ignore) {
			// select default tab in case of wrong user input
		}
	}
	
	dojo.connect(dijit.byId("tab2"), "onDownloadEnd", galaxytool_tab2_loaded );
	
	dojo.subscribe("tabcontainer-selectChild", function(child) {
		window.location.hash = "#select_" + child.get("id");

		// disable message area
		dojo.byId("messageArea").style.display  = "none";
	});

}

dojo.ready(galaxytool_status_init);